package com.gaofei;

import com.gaofei.api.FeginKafka;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@SpringBootApplication
@RestController
@EnableFeignClients
@RequestMapping("user")
@EnableTransactionManagement
public class UserApp {

    public static void main(String[] args) {
        SpringApplication.run(UserApp.class);
    }

    @Autowired
    FeginKafka feginKafka;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping("getKafka")
    public String getKafka(){

        return feginKafka.index();
    }

    @RequestMapping("sendKafka")
    @Transactional
    public String sendKafka(){

//        kafkaTemplate.send("2102A", UUID.randomUUID().toString(), "this is a message!!!!!!");
        kafkaTemplate.executeInTransaction(o->{
           o.send("2102A", UUID.randomUUID().toString(), "this is a message!!!!!!");
            throw new RuntimeException("出现了异常");
        });
//        throw new RuntimeException("出现了异常");
        return "success!";
    }
}
